﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.BusinessLayer.BLL;
using JKF.Entities.Model;
using JKF.Entities;
using System.Data;
namespace JKF.HandlerLayer.Product
{
    public partial class Product_SortHandler:AHttpHandler
    {
        private static bllPRODUCT_SORT bllProduct_Sort = new bllPRODUCT_SORT();
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }
        /// <summary>
        /// 添加产品分类
        /// </summary>
        public void InsertProduct_Sort()
        {
            try
            {
                var strScr = P("moduleObj");
                var product_SortInfo = this.BuildProduct_SortinfoByJson(JsonSerializer.Deserialize<PRODUCT_SORTinfo>(strScr), false);
                var tid = product_SortInfo.SORT_KEY;
                //return;
                bllProduct_Sort.Add(product_SortInfo);
                CacheManage.Remove("Product_SortOne"); //移除缓存
                var pk = product_SortInfo.SORT_KEY.ToString();
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "添加成功.", Data = pk }));   
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
        /// <summary>
        /// 修改产品分类
        /// </summary>
        public void EditProduct_Sort()
        {
            try
            {
                var strScr = P("moduleObj");
                var product_SortInfo = this.BuildProduct_SortinfoByJson(JsonSerializer.Deserialize<PRODUCT_SORTinfo>(strScr), true);
                var tid = product_SortInfo.SORT_KEY;
                bllProduct_Sort.Update(product_SortInfo);
                CacheManage.Remove("Product_SortOne"); //移除缓存
                var pk = product_SortInfo.SORT_KEY.ToString();
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "修改成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
        /// <summary>
        /// 从Json对象构建对象
        /// </summary>
        /// <param name="shopInfo">实体对象</param>
        /// <param name="isEdit">是否为编辑</param>
        /// <returns></returns>
        public PRODUCT_SORTinfo BuildProduct_SortinfoByJson(PRODUCT_SORTinfo product_sortInfo, bool isEdit)
        { 
            var entity = new PRODUCT_SORTinfo();
            if (!isEdit) // 添加
            {
                entity.SORT_KEY = new Guid(DB.GetNewGuid(DB.HR)); //Guid.NewGuid();
            }
            else // 修改
            {
                entity.SORT_KEY = product_sortInfo.SORT_KEY ;
            }
            entity.NAME = product_sortInfo.NAME;
            entity.TYPE = product_sortInfo.TYPE;
            entity.USER_CREATE = bllSYS_USER.CurrentUser.USER_KEY; // 获取当前用户guid
            entity.DATE_CREATE = System.DateTime.Now;
            return entity;
        }
        /// <summary>
        /// 获取产品类别列表 GetProduct_Sorts
        /// </summary>
        public void GetProduct_Sorts()
        {
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in bllProduct_Sort.GetProduct_SortsBLL(P("NAME"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {

                             SORT_KEY = a.Field<Guid>("SORT_KEY"),
                             NAME = a.Field<string>("NAME"),
                             //TYPE= a.Field<string>("TYPE")
                             TYPE = Product_Sort_Type(a.Field<string>("TYPE"))
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }
        /// <summary>
        /// 根据当前产品所属分类值，转成字符串
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private string Product_Sort_Type(string type)
        {
            switch (type)
            {
                case "1":
                    return "合同类";
                case "2":
                    return "耗材类";
                case "3":
                    return "增值系列";
                default:
                    return "未知";
            }
        }
        /// <summary>
        /// 删除产品类别
        /// </summary>
        public void DeleteProduct_Sort()
        {
            try
            {
                var sort_key = P("id");

                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = bllProduct_Sort.TransactionSQLDeleteBLL(new Guid(sort_key)), Data = sort_key }));
                CacheManage.Remove("Product_SortOne"); //移除缓存
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
        /// <summary>
        /// 根据ID获取产品类别实体
        /// </summary>
        public void GetProduct_SortByCode()
        {
            try
            {
                var scid = P("moduleKey");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllProduct_Sort.GetModel(new Guid(scid)) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
        /// <summary>
        /// 检查已输入名称是否存在
        /// </summary>
        public void CheckProduct_SortName()
        {
            try
            {
                var product_sort_name = P("Product_Sort_NAME");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllProduct_Sort.ExistsProduct_SortNameBLL(product_sort_name) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
        /// <summary>
        /// 根据当前TYPE值获取其Select列表 Utils.ToClient(false, sb.ToString());
        /// </summary>
        /// <returns></returns>
        public void GetProduct_SortByType()
        {
            try
            {
                string type = base.P("TYPE"); 
                 Utils.ToClient(false,bllProduct_Sort.GetProduct_SortByTypeBLL(type));
            }
            catch (Exception ex)
            {
                Utils.ToClient(true, ex.Message); 
            } 
        }
    }
}
